#include "DLL.h"

COMMAND command;
HANDLE clientPipe;
DWORD n;
INT response;

VOID CreateCommunication(TCHAR *IP, COMMUNICATION_INFO *pipesInfo)
{
	TCHAR info[50];
#ifdef UNICODE 
	_setmode(_fileno(stdin), _O_WTEXT);
	_setmode(_fileno(stdout), _O_WTEXT);
#endif

	_stprintf_s(info,TEXT("\\\\%s%s"),IP,MAINPIPE);
	if (!WaitNamedPipe(info, NMPWAIT_WAIT_FOREVER))
		exit(1);

	clientPipe = CreateFile(info, GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (clientPipe == NULL)
		exit(1);

	Sleep(100);

	_stprintf_s(info,TEXT("\\\\%s%s"),IP,UPDATEUSERSPIPE);
	if (!WaitNamedPipe(info, NMPWAIT_WAIT_FOREVER))
		exit(1);

	pipesInfo->updateUsersPipe = CreateFile(info, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (pipesInfo->updateUsersPipe == NULL)
		exit(1);

	Sleep(100);

	_stprintf_s(info,TEXT("\\\\%s%s"),IP,PUBLICMESSAGEPIPE);
	if (!WaitNamedPipe(info, NMPWAIT_WAIT_FOREVER))
		exit(1);

	pipesInfo->publicMessagesPipe = CreateFile(info, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (pipesInfo->publicMessagesPipe == NULL)
		exit(1);

	Sleep(100);

	_stprintf_s(info,TEXT("\\\\%s%s"),IP,PRIVATEMESSAGEPIPE);
	if (!WaitNamedPipe(info, NMPWAIT_WAIT_FOREVER))
		exit(1);

	pipesInfo->privateMessagesPipe = CreateFile(info, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (pipesInfo->privateMessagesPipe == NULL)
		exit(1);

	Sleep(100);

	_stprintf_s(info,TEXT("\\\\%s%s"),IP,WARNINGPIPE);
	if (!WaitNamedPipe(info, NMPWAIT_WAIT_FOREVER))
		exit(1);

	pipesInfo->warningPipe = CreateFile(info, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (pipesInfo->warningPipe == NULL)
		exit(1);
}
VOID CloseCommunication()
{
	command.message = CLOSECOMMUNICATION;
	
	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);
}

INT Login(TCHAR *login, TCHAR *pass)
{
	command.message = LOGIN;
	_stprintf_s(command.arguments[0],login);
	_stprintf_s(command.arguments[1],pass);

	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);

	if(!ReadFile(clientPipe, &response, sizeof(response), &n, NULL))
		exit(1);

	return response;
}
VOID Logout()
{
	command.message = LOGOUT;
	
	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);
}

PUBLICHISTORY GetPublicHistory()
{
	PUBLICHISTORY publicMessages;

	command.message = GETPUBLICHISTORY;

	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);

	if(!ReadFile(clientPipe, &publicMessages, sizeof(PUBLICHISTORY), &n, NULL))
		exit(1);

	return publicMessages;
}
PRIVATEHISTORY GetPrivateHistory(TCHAR *withUser)
{
	PRIVATEHISTORY privateMessages;

	command.message = GETPRIVATEHISTORY;
	_stprintf_s(command.arguments[0],withUser);

	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);

	if(!ReadFile(clientPipe, &privateMessages, sizeof(PRIVATEHISTORY), &n, NULL))
		exit(1);

	return privateMessages;
}

VOID SendInvitation(TCHAR *user)
{
	command.message = SENDINVITATION;
	_stprintf_s(command.arguments[0],user);

	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);
}
INT StartConversation(TCHAR *user)
{
	command.message = STARTCONVERSATION;
	_stprintf_s(command.arguments[0],user);

	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);

	if(!ReadFile(clientPipe, &response, sizeof(response), &n, NULL))
		exit(1);

	return response;
}
VOID FinishConversation()
{
	command.message = FINISHCONVERSATION;

	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);
}

INT SendPrivateMessage(TCHAR *message)
{
	command.message = SENDPRIVATEMESSAGE;
	_stprintf_s(command.arguments[0],message);

	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);

	if(!ReadFile(clientPipe, &response, sizeof(response), &n, NULL))
		exit(1);

	return response;
}
VOID SendPublicMessage(TCHAR *message)
{
	command.message = SENDPUBLICMESSAGE;
	_stprintf_s(command.arguments[0],message);

	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);
}

//ADMINISTRATION
INT GetAllUsers(USER *users)
{
	INT number;
	command.message = GETREGISTEREDUSERS;

	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);

	if(!ReadFile(clientPipe, users, USERS_NBR*sizeof(USER), &n, NULL))
		exit(1);

	if(!ReadFile(clientPipe, &number, sizeof(INT), &n, NULL))
		exit(1);

	return number;
}
INT CreateAccount(TCHAR *login, TCHAR *pass)
{
	command.message = CREATE;
	_stprintf_s(command.arguments[0],login);
	_stprintf_s(command.arguments[1],pass);

	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);
	
	if(!ReadFile(clientPipe, &response, sizeof(response), &n, NULL))
		exit(1);

	return response;
}
INT RemoveAccount(TCHAR *user)
{
	command.message = REMOVE;
	_stprintf_s(command.arguments[0],user);

	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);
	
	if(!ReadFile(clientPipe, &response, sizeof(response), &n, NULL))
		exit(1);

	return response;
}
VOID Shutdown()
{
	command.message = SHUTDOWN;
	
	if (!WriteFile(clientPipe, &command, sizeof(COMMAND), &n, NULL))
		exit(1);
}
